package goldensubmarine.skoda.yeti.view.layers.content.pages.page3D.five3d
{
	import br.com.stimuli.loading.BulkLoader;
	import com.greensock.easing.Back;
	import com.greensock.TweenMax;
	import com.nuke.player.Player;
	import com.nuke.text.FontTextField;
	import com.nuke.utils.Library;
	import com.nuke.utils.Mirror;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import msz.framework.events.PagesEvent;
	import net.badimon.five3D.display.Bitmap3D;
	import net.badimon.five3D.display.MovieClip3D;
	import net.badimon.five3D.display.Sprite3D;
	
	/**
	 * ...
	 * @author Bartłomiej JERZY
	 */
	public class CarouselItem extends Sprite3D
	{
		public static var WIDTH						:int = 590
		public static var HEIGHT					:int = 332
		
		private var _t						:Number 
		private var _n						:CarouselItem 
		private var _p						:CarouselItem 
		private var _data					:Object
		
		private var _prevX					:Number
		private var _prevY					:Number
		private var _prevZ					:Number
		
		private var _x						:Number = 0
		private var _y						:Number = 0 
		private var _z						:Number = 0
		
		private var _bitmap					:Bitmap3D
				
		private var _bmp					:BitmapData
		
		private var _sign					:MovieClip = new MovieClip()
		private var _signTxt				:FontTextField = new FontTextField({font:"SkodaSans",size:30,color:0x373737,bold:true},{autoSize:"left",width:WIDTH,multiline:true,wordWrap:true})
		private var _ms3d					:MovieClip3D 
		
		private var _hit					:Sprite3D = new Sprite3D()
		private var _cont					:Sprite3D = new Sprite3D()
		private var _bg						:Sprite3D= new Sprite3D()
		private var _enabled 				:Boolean = true
		
		private var _preloader				:MovieClip3D 
		
		private var _loader					:BulkLoader = BulkLoader.getLoader("main")
		private var _titleHidden			:Boolean = false
		private var _over					:Boolean = false
		
		public function CarouselItem($data:Object = null) 
		{
			
		
			if($data) _data = $data
			trace("### DATA ###",_data.image[0])
			init()
			
			
		}
		
		private function init():void
		{
			initBg()
			addPreloader()
			loadImage()
			addSign()
		}
		
		
		private function initBg():void
		{
			_bg.graphics3D.beginFill(0xffffff)
			_bg.graphics3D.drawRect(0,0,WIDTH+20,HEIGHT+20)
			_bg.graphics3D.endFill()
			
			_cont.addChild(_bg)	
			
		}
				
		private function loadImage():void {
			
			if ( _data  && String(_data.image[0]) != "") {
				_loader.add(String(_data.image[0]),{id:_data.image[0]})
				
				if(!_loader.get(_data.image).isLoaded){
					_loader.get(_data.image).addEventListener(Event.COMPLETE, imageLoaded)
					_loader.start()
				}else {
					imageLoaded()
				}
			}else {
				_bmp = new BitmapData(WIDTH,HEIGHT,false,0xffff0000)
				imageLoaded()
			}
		}
		
		private function imageLoaded(e:Event = null):void {
			if (_data && _loader.get(String(_data.image[0]))) {
				_bmp = _loader.getBitmapData(String(_data.image[0]))
				_loader.get(String(_data.image[0])).removeEventListener(Event.COMPLETE,imageLoaded)
			}
			removePreloader()
			initBitmaps()
		}
		
		private function initBitmaps():void {
			_hit.graphics3D.beginFill(0xffffff,0)
			_hit.graphics3D.drawRect(0, 0, WIDTH, HEIGHT)
			_hit.graphics3D.endFill()
			_hit.x = 10
			_hit.y = 10
			
		
			_hit.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler)
			_hit.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler)
			_hit.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler)
			_hit.buttonMode  = true
		
			_bitmap = new Bitmap3D(_bmp, true)
			_bitmap.x = (WIDTH+20-_bmp.width)/2
			_bitmap.y = (HEIGHT + 20 - _bmp.height) / 2
			_bitmap.z = -1
			//_bitmap.alpha = 0
					
							
			_cont.x = -(WIDTH+20) / 2
			_cont.y = -(HEIGHT+20) / 2
			
			
			_cont.addChild(_bitmap)	
			_cont.addChild(_hit)
			
			addChild(_cont)
		}
		
		private function addPreloader():void {
			var _mc:MovieClip = Library.getMovieClip("preloader")
			_preloader =  new MovieClip3D(_mc,_mc.getRect(_mc),true)
			_preloader.x = WIDTH/2
			_preloader.y = HEIGHT/2
			_preloader.activate()
			_cont.addChild(_preloader)
		}
		
		private function addSign():void {
			_signTxt.htmlText = _data.@name
			_signTxt.x = 20
			_sign.addChild(_signTxt)
			_sign.graphics.beginFill(0xffffff)
			_sign.graphics.drawRect(0,0,WIDTH+20,_signTxt.height+20)
			_sign.graphics.endFill()
			
			
			
			_ms3d = new MovieClip3D(_sign, _sign.getRect(_sign), true)
			_ms3d.y = HEIGHT + 20
			_ms3d.refresh()
			_ms3d.alpha = 0
			_ms3d.rotationX = 90
			_cont.addChildAt(_ms3d,0)
		}
		
		private function removePreloader():void {
			if (_preloader) {
				_preloader.deactivate()
				_cont.removeChild(_preloader)
				_preloader = null
			}
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			dispatchEvent(new Event(Event.SELECT, true))
		}
		
		private function mouseClickHandler(e:MouseEvent):void 
		{
			dispatchEvent(new Event(Event.SELECT, true))
		}
		
		private function mouseOverHandler(e:MouseEvent):void 
		{
			_over = true
			if(!_titleHidden) TweenMax.to(_ms3d,0.3,{rotationX:0,alpha:1})
		}
		
		private function mouseOutHandler(e:MouseEvent):void 
		{
			_over = false
			if(!_titleHidden)  TweenMax.to(_ms3d,0.4,{rotationX:90,alpha:0})
		}
		
		public function hideTitle($b:Boolean):void {
			if ($b) {
				_hit.removeEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler)
				_hit.removeEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler)
				TweenMax.to(_ms3d,0.2,{alpha:0})
			}else {
				_hit.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler)
				_hit.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler)
				if(_over) TweenMax.to(_ms3d,0.4,{alpha:1})
			}
		}
		
		public function hideAndClear():void {
			enabled = false
			
			if (_bitmap) {
				TweenMax.to(_cont,0.3,{y:_cont.y-800,autoAlpha:0,onComplete:destroy})
				
			}else {
				destroy()
			}
		}
		
		
		public function destroy():void {
			removeEventListener(Event.ADDED_TO_STAGE,init)
		
			_hit.removeEventListener(MouseEvent.CLICK,mouseClickHandler)
			
			if (_bitmap) {
				_cont.removeChild(_hit)
				_cont.removeChild(_ms3d)
				if(_preloader) _cont.removeChild(_preloader)
				removeChild(_cont)
			}
			
			
			_cont = null
			
			_bitmap = null
			_preloader = null
			_ms3d = null
			
			dispatchEvent(new PagesEvent(PagesEvent.ON_CLEAR))
		}
		
		public function get t():Number { return _t; }
		
		public function set t(value:Number):void 
		{
			if(_t>1) _t = 0.999
			if (_t < 0) _t = 0.001
			_t = value;
		}
		
		public function get p():CarouselItem { return _p; }
		
		public function set p(value:CarouselItem):void 
		{
			_p = value;
		}
		
		public function get n():CarouselItem { return _n; }
		
		public function set n(value:CarouselItem):void 
		{
			_n = value;
		}
		
		public function get prevX():Number { return _prevX; }
		
		public function set prevX(value:Number):void 
		{
			_prevX = value;
		}
		
		public function get prevY():Number { return _prevY; }
		
		public function set prevY(value:Number):void 
		{
			_prevY = value;
		}
		
		public function get prevZ():Number { return _prevZ; }
		
		public function set prevZ(value:Number):void 
		{
			_prevZ = value;
		}
		
		override public function set y(value:Number):void 
		{
			if(value != _y) _prevY = _y;
			_y = value
			super.y = _y
			
		}
		
		
		override public function set x(value:Number):void 
		{
			if(value != _x) _prevX = _x;
			_x = value
			super.x = _x
		}
		
		override public function set z(value:Number):void 
		{
			if(value != _z) _prevZ = _z;
			_z = value
			super.z = _z
			
		}
		
		public function get enabled():Boolean { return _enabled; }
		
		public function set enabled(value:Boolean):void 
		{
			_enabled = value;
		}
		
		public function get data():Object { return _data; }
		
		
		override public function set rotationY($v:Number):void {
			super.rotationY = $v
			/*if ($v > 60 || $v < -60) {
				_cont.setChildIndex(_bg,_cont.numChildren-1)
			}else {
				_cont.setChildIndex(_bg,0)
			}*/
		}
		
	}

}